package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Company;
import cn.edu.scau.dropship.entity.User;
import cn.edu.scau.dropship.service.CompanyService;
import cn.edu.scau.dropship.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;

    //根据用户名查找用户类型
    @PostMapping(value = "/findUserTypeByName")
    @ResponseBody
    public String findUserTypeByName(HttpServletRequest request){
        String uname=request.getParameter("uname");
        List<User> list=userService.selectAll();
        for(User user:list){
            if(user.getName().equals(uname)){
                return user.getAccountType();
            }
        }
        return "false";
    }

    //获取管理员信息
    @PostMapping(value = "/login")
    @ResponseBody
    public List<Object> login(HttpServletRequest request){
        //登陆结果
        List<Object> list=new ArrayList<>();

        String name=request.getParameter("name");
        String pw=request.getParameter("pw");

        User user=userService.selectByName(name);
        if(user==null){
            list.add(user);
            list.add("用户不存在!");
        }
        String real_pw=user.getPassword();
        if(!real_pw.equals(pw)){
            list.add(user);
            list.add("密码错误！");
        }
        list.add(user);
        list.add("true");

        return list;
    }

    //根据用户类型查找用户
    @RequestMapping("/selectUsers")
    @ResponseBody
    public String selectUsers(HttpServletRequest request){
        String account_type = request.getParameter("account_type");
        List<User> userList = userService.selectByType(account_type);
        request.setAttribute("userList",userList);
        return "success";
    }

    @RequestMapping("/updateUserType")
    @ResponseBody
    public String updateUserType(HttpServletRequest request){
        String id = request.getParameter("id");
        String account_type = request.getParameter("account_type");
        User user = userService.selectByPrimaryKey(Integer.parseInt(id));
        user.setAccountType(account_type);
        int result=userService.updateByPrimaryKey(user);
        if(result<=0){
            return "fail";
        }
        return "success";
    }

    @RequestMapping("/getUserAll")
    @ResponseBody
    public List<User> getUserAll(){
        List<User> userList = userService.selectAll();
        return userList;
    }

    @RequestMapping("/getUserAllNotAdmin")
    @ResponseBody
    public List<User> getUserAllNotAdmin(HttpServletRequest request){
        List<User> result = new ArrayList<>();
        Integer admin_id=Integer.valueOf(request.getParameter("uid"));

        List<User> userList = userService.selectAll();
        for(User user:userList){
            if(user.getId()!=admin_id){
                result.add(user);
            }
        }
        return result;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(HttpServletRequest request){
        String id = request.getParameter("id");
        int result=userService.deleteByPrimaryKey(Integer.parseInt(id));
        if(result<=0){
            return "用户删除失败!";
        }
        return "删除用户成功!";
    }


    //根据uid查用户信息
    @PostMapping("/selectById")
    @ResponseBody
    public User selectById(HttpServletRequest request){
        String uid = request.getParameter("uid");
        User user=userService.selectByPrimaryKey(Integer.valueOf(uid));
        return user;
    }

    //修改用户信息
    @PostMapping("/updateUser")
    @ResponseBody
    public String updateUser(HttpServletRequest request){
        /*Integer uid = Integer.valueOf(request.getParameter("uid"));
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");

        User user=userService.selectByPrimaryKey(uid);
        user.setEmail(email);
        user.setPhone(phone);
        int result=userService.updateByPrimaryKey(user);
        if (result<=0){
            return "false";
        }
        return "true";*/


        Integer uid=Integer.valueOf(request.getParameter("uid"));
        User user=userService.selectByPrimaryKey(uid);

        //user.setName(request.getParameter("name"));
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        if(email.length()>30){
            return "false";
        }
        if(phone.length()>11){
            return "false";
        }
        user.setEmail(email);
        user.setPhone(phone);

        int result=userService.updateByPrimaryKey(user);
        if(result>0){
            return "true";
        }
        return "false";
    }


    @PostMapping("/selectCompanyByUid")
    @ResponseBody
    public Object selectCompanyByUid(Integer id){
        User user = userService.selectByPrimaryKey(id);
        Integer cid = user.getCompanyId();
        if(cid==null){
            return "false";
        }
        Company company = companyService.selectByPrimaryKey(cid);
        return company;
    }
}
